import React, { useState } from 'react'
import { Alert, Button, Form, Input, message } from 'antd'
import { useRequest, useTimeout } from 'ahooks'
import style from './style.scss'
import { apiAccountLogin } from '@/apis/account.api'
import { useHistory } from 'react-router'

const LoginPage = () => {
  const history = useHistory()
  const [state, setState] = useState(1)
  const { run, loading, error } = useRequest(apiAccountLogin, {
    manual: true,
    throwOnError: true,
    onSuccess: async (res) => {
      if (res.status) {
        message.success('登录成功')

        // const { data } = await apiGetCurrentInfo();

        // setInitialState({
        //   ...initialState,
        //   userinfo: data.info,
        //   policys: data.policys,
        // });

        history.push('/')
      }
    }
  })

  useTimeout(() => {
    setState(state + 1)
  }, 3000)

  return (
    <div className={style.bg}>
      <div className={style.panel}>
        <div className={style.main}>
          <div className={style.dialog}>
            <h3 className={style.title}>后台登陆</h3>

            {error && <Alert className={style.error} message={error} type='error' showIcon />}

            <Form
              layout='vertical'
              scrollToFirstError
              onFinish={v => run(v)}
            >
              <Form.Item
                label='账号'
                name='username'
                rules={[
                  {
                    required: true,
                    message: '管理账号必填'
                  }
                ]}
              >
                <Input placeholder='输入管理账号' />
              </Form.Item>

              <Form.Item
                label='密码'
                name='password'
                rules={[
                  {
                    required: true,
                    message: '请输入登录密码'
                  }
                ]}
              >
                <Input placeholder='输入登录密码' type='password' />
              </Form.Item>

              <Form.Item>
                <Button
                  block
                  htmlType='submit'
                  loading={loading}
                  type='primary'
                >
                  登录
                </Button>
              </Form.Item>
            </Form>
          </div>
        </div>
      </div>
    </div>
  )
}

export default LoginPage
